package com.saltedfish.tool.dialog;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.app.NotificationManagerCompat;

import com.saltedfish.tool.R;
import com.saltedfish.tool.dao.AppDatabase;
import com.saltedfish.tool.dao.DayDBDao;
import com.saltedfish.tool.dao.SubFlagModelDao;
import com.saltedfish.tool.listener.ChineseInputFilter;
import com.saltedfish.tool.model.DayTable;
import com.saltedfish.tool.model.ModelInfo;
import com.saltedfish.tool.model.SmallTable;
import com.saltedfish.tool.util.CalendarReminderUtils;
import com.saltedfish.tool.util.DateUtils;
import com.saltedfish.tool.util.UIUtils;
import com.saltedfish.tool.view.SwitchButton;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.base.SimpleListener;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.popwindow.DialogHelper;
import cn.droidlover.xdroidmvp.util.AppUtil;
import cn.droidlover.xdroidmvp.util.ToastUtils;

public class UICreateSonFlagDialog extends DialogHelper {

    @BindView(R.id.ed_flag_titile)
    EditText ed_flag_titile;

    @BindView(R.id.tvTitle)
    TextView tvTitle;

    @BindView(R.id.tvBack)
    TextView tvBack;

    @BindView(R.id.tvSave)
    TextView tvSave;

    @BindView(R.id.tvRepeatDate)
    TextView tvRepeatDate;

    @BindView(R.id.tvFlagStartDate)
    TextView tvFlagStartDate;

    @BindView(R.id.tvWeek)
    TextView tvWeek;

    @BindView(R.id.tvWeekDate)
    TextView tvWeekDate;

    @BindView(R.id.tvFlagEndDate)
    TextView tvFlagEndDate;

    @BindView(R.id.releateFlagStartDate)
    RelativeLayout releateFlagStartDate;

    @BindView(R.id.releateFlagEndDate)
    LinearLayout releateFlagEndDate;

    @BindView(R.id.releateProgressStatus)
    LinearLayout releateProgressStatus;

    @BindView(R.id.releateFlagWeekDate)
    RelativeLayout releateFlagWeekDate;

    @BindView(R.id.tvProgressStatus)
    TextView tvProgressStatus;

    @BindView(R.id.switchButton)
    SwitchButton switchButton;

    @BindView(R.id.linearEditText)
    LinearLayout linearEditText;

    @BindView(R.id.ivSonFlagRight1)
    ImageView ivSonFlagRight1;

    @BindView(R.id.ivSonFlagRight2)
    ImageView ivSonFlagRight2;

    @BindView(R.id.ivSonFlagRight3)
    ImageView ivSonFlagRight3;

    @BindView(R.id.mLineaCreateFlag)
    LinearLayout mLineaCreateFlag;

    private String flagId;

    private String subId;

    private SubFlagModelDao subFlagModelDao ;
    private DayDBDao dayDBDao ;


    private boolean isRemind = true;

    //重新编辑的时候，记录上一次是不是有提醒功能
    private boolean isReminds = false;
    private String titles = "";

    public UICreateSonFlagDialog(Activity context, String flagId, AppDatabase database) {
        super(context, R.style.AlertDialogStyle);
        this.mContext = context;
        this.flagId = flagId;
        this.subFlagModelDao = database.subFlagDao();
        this.dayDBDao = database.dayDBDao();
        setFullWidthScreen();
        setGravity(Gravity.BOTTOM);
        setAnimSet(R.style.popBottomPopAnim);
        initSubFlagData();

    }

    private  int startT;
    private  int day;

    public void updateFlag(String subIds,String flagIds) {
        subId = subIds;
        flagId = flagIds;
        ed_flag_titile.requestFocus();
        ed_flag_titile.setFocusable(true);
        tvTitle.setText("创建子目标");
        if(!TextUtils.isEmpty(subIds) && subFlagModelDao !=null){
            tvTitle.setText("编辑子目标");
            releateProgressStatus.setVisibility(View.VISIBLE);
            subFlagModel = subFlagModelDao.getBySubId(subId);
            if(subFlagModel !=null){
                titles = subFlagModel.title;
                ed_flag_titile.setText(subFlagModel.title);
                ed_flag_titile.setSelection(subFlagModel.title.length());
                String startTime = DateUtils.strToDateStr(subFlagModel.startTime);
                tvFlagStartDate.setText(DateUtils.getStringByDate(startTime));
                if(TextUtils.isEmpty(subFlagModel.repeatDay)){
                    tvWeek.setText("不重复");
                    tvWeek.setTextColor(Color.parseColor("#3097FF"));
                    releateFlagEndDate.setVisibility(View.GONE);
                }else {
                    releateFlagEndDate.setVisibility(View.VISIBLE);
                    tvFlagEndDate.setText(DateUtils.strToDateStr(subFlagModel.endTime));
                    String[] weeks = subFlagModel.repeatDay.split(" ");
                    tvWeek.setTextColor(Color.parseColor("#333333"));
                    if(weeks.length != 7){
                        tvWeek.setText(subFlagModel.repeatDay);
                    }else {
                        tvWeek.setText("每天");
                    }
                }
                tvWeekDate.setText(subFlagModel.repeatTime);
                isRemind = subFlagModel.isRemind;
                isReminds = subFlagModel.isRemind;
                status = subFlagModel.status;
                if(subFlagModel.isRemind){
                    switchButton.setChecked(true);
                }else {
                    switchButton.setChecked(false);
                }

                startT =  DateUtils.compareEqualsDate(subFlagModel.startTime, DateUtils.getStringDateShort());

                day =  DateUtils.compareEqualsDate(subFlagModel.endTime,DateUtils.getStringDateShort());
                //进行的状态
              //  startT =  DateUtils.getDays(subFlagModel.startTime,DateUtils.getStringDateShort());
                if(startT != 0){
                    ivSonFlagRight1.setVisibility(View.GONE);
                    ivSonFlagRight3.setVisibility(View.VISIBLE);
                    if(day != 1){
                        ivSonFlagRight2.setVisibility(View.VISIBLE);
                        tvProgressStatus.setTextColor(mContext.getResources().getColor(R.color.color_333333));
                         if(status.equals("暂停")){
                            tvProgressStatus.setText("暂停");
                            tvProgressStatus.setTextColor(mContext.getResources().getColor(R.color.color_3097FF));

//                        }else  if(status.equals("noStart")){
//                            tvProgressStatus.setText("暂未开始");
//                        }else  if(status.equals("delay")){
//                            tvProgressStatus.setText("已延期");
                        }else  if(status.equals("提前完成")){
                            tvProgressStatus.setText("提前完成");
                        }else {
                             status = "进行中";
                            tvProgressStatus.setText("进行中");
                        }
                    }else{
                        ivSonFlagRight2.setVisibility(View.GONE);
                        status = "已延期";
                        tvProgressStatus.setText("已延期");
                        tvProgressStatus.setTextColor(mContext.getResources().getColor(R.color.color_ED5656));
                    }
                }else{
                    ivSonFlagRight1.setVisibility(View.VISIBLE);
                    ivSonFlagRight2.setVisibility(View.VISIBLE);
                    ivSonFlagRight3.setVisibility(View.GONE);
                    status = "暂未开始";
                    tvProgressStatus.setText("暂未开始");
                    tvProgressStatus.setTextColor(mContext.getResources().getColor(R.color.color_3097FF));
                }
            }
        }
    }

    private SimpleDateFormat formatterDay = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal;
    Date today;

    @Override
    protected void initData() {

    }

    private void initSubFlagData() {
        subFlagModel = new SmallTable();
        releateProgressStatus.setVisibility(View.GONE);
        cal = Calendar.getInstance();
        today = cal.getTime();
        cal.add(Calendar.DATE, 29);
        Date endDays = cal.getTime();
        subFlagModel.startTime = formatterDay.format(today);
        subFlagModel.endTime = formatterDay.format(endDays);
        subFlagModel.repeatDay = "周一 周二 周三 周四 周五";
        mContext.getWindow().setStatusBarColor(mContext.getResources().getColor(R.color.color_white));
        subFlagModel.repeatTime = "19:00-20:00";
        ed_flag_titile.setText("");
        tvFlagStartDate.setText("今天");
        tvFlagEndDate.setText(DateUtils.strToDateStr(subFlagModel.endTime));
        tvProgressStatus.setText("");
        tvWeekDate.setText(subFlagModel.repeatTime);
        tvWeek.setText(subFlagModel.repeatDay);
        tvWeek.setTextColor(Color.parseColor("#333333"));

        releateFlagEndDate.setVisibility(View.VISIBLE);
        switchButton.setChecked(true);
        isRemind = true;

    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_ui_create_son_flag;
    }


    @Override
    protected void initView() {

        tvRepeatDate.setText("重复&时间");

        InputFilter[] filter = {new ChineseInputFilter(28)};
        ed_flag_titile.setFilters(filter);
        ed_flag_titile.setHint(" 打算立个什么子目标？");
        ed_flag_titile.requestFocus();
        ed_flag_titile.setFocusable(true);   //设置可以获取焦点
        ed_flag_titile.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        ed_flag_titile.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI|EditorInfo.IME_ACTION_DONE);

//        ed_flag_titile.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId,
//                                          KeyEvent event) {
//                if (actionId == EditorInfo.IME_ACTION_DONE) {
////                    XLog.e("-----------打算立个什么子目标------");
//                }
//                return false;
//            }
//        });

        switchButton.toggle(true);

        switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                isRemind = isChecked;
            }
        });
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveCreateSonFlag();
            }
        });

        ed_flag_titile.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    ed_flag_titile.setHintTextColor(mContext.getResources().getColor(R.color.color_3097FF));
                    ed_flag_titile.setHint(" 打算立个什么子目标？");
                }else{
                    ed_flag_titile.setHintTextColor(mContext.getResources().getColor(R.color.color_CCCCCC));
                    ed_flag_titile.setHint("打算立个什么子目标？");
                }
            }
        });

        releateFlagStartDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(subId)){
                    if(startT != 0){
                        ToastUtils.showMessage(mContext,"仅暂未开始的子目标可修改开始日期");
                    }else{
                        type = 0;
                        initStatusDateView();
                    }
                }else {
                    type = 0;
                    initStatusDateView();
                }
            }
        });
        releateFlagEndDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(subId)){
                    if(day != 1){
                        type = 1;
                        initStatusDateView();
                    }else {
                        ToastUtils.showMessage(mContext,"仅未延期的子目标可修改结束日期");
                    }
                }else {
                    type = 1;
                    initStatusDateView();
                }
            }
        });

        releateProgressStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initStatusView();
            }
        });
        releateFlagWeekDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initWeekView();
            }
        });

        //监听软键盘是否显示或隐藏
        linearEditText.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        Rect r = new Rect();
                        linearEditText.getWindowVisibleDisplayFrame(r);
                        int screenHeight = linearEditText.getRootView()
                                .getHeight();
                        int heightDifference = screenHeight - (r.bottom);
                        if (heightDifference > 200) {
                            if(!flag){
                                ed_flag_titile.requestFocus();
                                flag = true;
                            }
                            //软键盘显示
                            // changeKeyboardHeight(heightDifference);
                        } else {
                            //软键盘隐藏
                            ed_flag_titile.clearFocus();

                        }
                    }

                });

    }

    private boolean flag = false;

    private  UIProgressStatusDialog statusDialog ;

    private UIDateStartDialog startDialog ;
    private UIDateEndDialog endDialog ;

    private  UIWeekDialog weekDialog ;

    private int type = 0 ; //0代表开始时间 1，是结束时间

    private SmallTable subFlagModel = new SmallTable();

    /**
     * 开始时间选择/结束时间选择
     */
    private void initStatusDateView() {
        if(type == 0){
            if(startDialog == null){
                startDialog = new UIDateStartDialog(mContext);
            }
            startDialog.setListener(new UIDateStartDialog.SimpleDateListener() {
                @Override
                public void onDateClick(SmallTable subFlagModels) {
//                XLog.e("--subFlagModel--->>"+subFlagModel.toString());
                    subFlagModel = subFlagModels;
                    String startTime = DateUtils.strToDateStr(subFlagModel.startTime);
                    tvFlagStartDate.setText(DateUtils.getStringByDate(startTime));
                }
            });
            startDialog.setOnKeyBackListener(false);
            startDialog.updateDate(subFlagModel);
            startDialog.show();
        }else {
            if(endDialog == null){
                endDialog = new UIDateEndDialog(mContext);
            }
            endDialog.setListener(new UIDateEndDialog.SimpleDateListener() {
                @Override
                public void onDateClick(SmallTable subFlagModels) {
//                XLog.e("--subFlagModel--->>"+subFlagModel.toString());
                    subFlagModel = subFlagModels;
                    String endTime = DateUtils.strToDateStr(subFlagModel.endTime);
                    tvFlagEndDate.setText(DateUtils.getStringByDate(endTime));
                }
            });
            endDialog.setOnKeyBackListener(false);
            endDialog.updateDate(subFlagModel);
            endDialog.show();
        }

    }



    /**
     * 状态选择
     * //case ing = "进行中"
     *         //    case stop = "暂停"
     *         //    case noStart = "暂未开始"
     *         //    case advance = "提前完成"
     *         //    case delay = "已延期"
     */
    private void initStatusView() {

        if(subFlagModel !=null){
            if(subFlagModel.status.equals("暂未开始")){
                ToastUtils.showMessage(mContext,"仅已开始的子目标可修改进行状态");
            }else {
                if(statusDialog == null){
                    statusDialog = new UIProgressStatusDialog(mContext);
                    statusDialog.setSimpleListener(new SimpleListener() {
                        @Override
                        public void onClick(String index) {
                            status = index;
//                            XLog.e("-----状态选择-------"+index);
                            if(index.equals("进行中")){
                                tvProgressStatus.setText("进行中");
                                tvProgressStatus.setTextColor(mContext.getResources().getColor(R.color.color_333333));
                            }else  if(index.equals("暂停")){
                                tvProgressStatus.setText("暂停");
                                tvProgressStatus.setTextColor(mContext.getResources().getColor(R.color.color_3097FF));
                            }else  if(index.equals("暂未开始")){
                                tvProgressStatus.setText("暂未开始");
                                tvProgressStatus.setTextColor(mContext.getResources().getColor(R.color.color_3097FF));
                            }else  if(index.equals("已延期")){
                                tvProgressStatus.setText("已延期");
                                tvProgressStatus.setTextColor(mContext.getResources().getColor(R.color.color_ED5656));
                            }else  if(index.equals("提前完成")){
                                tvProgressStatus.setText("提前完成");
                                tvProgressStatus.setTextColor(mContext.getResources().getColor(R.color.color_333333));
                            }
                        }
                    });
                }
                statusDialog.setOnKeyBackListener(false);
                statusDialog.updateStatus(status);
                statusDialog.show();
            }
        }


    }

    /**
     * 重复时间选择（星期和每天的时间节点）
     */
    private void initWeekView() {
        if(weekDialog == null){
            weekDialog = new UIWeekDialog(mContext);
            weekDialog.setListener(new UIWeekDialog.SimpleDateListener() {
                @Override
                public void onDateClick(SmallTable subFlagModels) {
//                    XLog.e("--subFlagModel--->>"+subFlagModel.toString());
                    subFlagModel = subFlagModels;
                    if(TextUtils.isEmpty(subFlagModels.repeatDay)){
                        tvWeek.setText("不重复");
                        tvWeek.setTextColor(Color.parseColor("#3097FF"));
                        releateFlagEndDate.setVisibility(View.GONE);
                       // subFlagModel.endTime = subFlagModel.startTime;
                    }else {
                        releateFlagEndDate.setVisibility(View.VISIBLE);
                        String[] weeks = subFlagModel.repeatDay.split(" ");
                        tvWeek.setTextColor(Color.parseColor("#333333"));
                        if(weeks.length != 7){
                            tvWeek.setText(subFlagModel.repeatDay);
                        }else {
                            tvWeek.setText("每天");
                        }
                    }
                    tvWeekDate.setText(subFlagModels.repeatTime);
                }

            });

        }
        weekDialog.setOnKeyBackListener(false);
        weekDialog.updateDate(subFlagModel,subFlagModelDao);
        weekDialog.show();

    }

    private String eventId = "";
    //判断今天是否打卡
    private boolean todayIsTick = false;

    private void saveCreateSonFlag() {
        String title = ed_flag_titile.getText().toString();
        if(isRemind){
            if (!isNotificationEnabled()) {
                gotoSet();
                return;
            }
        }
        boolean flagEdit = AppUtil.isKeyboardShown(mLineaCreateFlag);
        if(TextUtils.isEmpty(title)){
            UIUtils.setVibrator(mContext);
            ed_flag_titile.requestFocus();
            if(!flagEdit){
                AppUtil.showSoftInput(mContext);
            }

            return;
        }
        subFlagModel.title = title;
        subFlagModel.isRemind = isRemind;
        subFlagModel.fId = flagId;

        //不重复
        if(TextUtils.isEmpty(subFlagModel.repeatDay)){
             subFlagModel.endTime = subFlagModel.startTime;
        }else {
            int index = DateUtils.compareEqualsDate(subFlagModel.startTime, subFlagModel.endTime);
            if(index == 0){
                subFlagModel.endTime = DateUtils.getNextDay(subFlagModel.startTime,29);
            }
        }
        if(TextUtils.isEmpty(subId)) {
            long days = DateUtils.getDays(subFlagModel.startTime, DateUtils.getStringDateShort());
            if(days > 0){
                status = "暂未开始";
            }else {
                status = "进行中";
            }
        }
//        subFlagModel.completeDay = "";
        subFlagModel.status = status;
        //设置提前完成的时候
        if(status.equals("提前完成") || status.equals("暂停")){
            if(status.equals("提前完成") ){
                Calendar cal = Calendar.getInstance();
                 SimpleDateFormat formatterDays = new SimpleDateFormat("yyyy/MM/dd");
                subFlagModel.completeDay = formatterDays.format(cal.getTime());
            }
            if(!TextUtils.isEmpty(subId)) {
                eventId = subId;
                if(isReminds){
                    CalendarReminderUtils.deleteCalendarEvent(mContext,subId);
                }
            }else {
                int random = (int)(Math.random()*100);
                eventId = Long.toString(System.currentTimeMillis()) +random +"";
            }
        }else {
            updateCalendarReminder();
        }

        if(subFlagModelDao !=null){
            if(!TextUtils.isEmpty(subId)){
                if(dayDBDao !=null){
                    List<DayTable> dayLists =  dayDBDao.getById(subId);
                    for (DayTable dayTable : dayLists) {
                        if(dayTable.isTick){
                            dayTable.title = subFlagModel.title;
                            dayTable.subId = subId;
                            if(dayTable.day.equals(formatterDay.format(today))){
                                todayIsTick = true;
                            }else {
                                todayIsTick = false;
                            }
                            dayDBDao.update(dayTable);
                        }else{
                            dayDBDao.delete(dayTable);
                        }
                    }
                }
                insertDayDBTable(subId);

            }else { //新增
                insertDayDBTable("");
            }
        }

        //提前完成的时候会有完成子目标或者完成Flag的弹出框
        if (listener != null) {
            listener.onClick(subFlagModel);
        }

        dismiss();

    }


    /**
     * //  重新编辑的时候，
     *         //  1.当之前是有通知的，现在还有通知，就是更新日历
     *         //  2.之前有通知，现在没有了，就是删除子目标日历
     */
    private void updateCalendarReminder() {
        if(!TextUtils.isEmpty(subId)) {
            eventId = subId;
            if(isReminds){
                if(subFlagModel.isRemind){
                    CalendarReminderUtils.updateCalendarEvent(mContext,subFlagModel);
                }else {
                    CalendarReminderUtils.deleteCalendarEvent(mContext,subId);
                }
            }else {
                if(subFlagModel.isRemind){
                    eventId = CalendarReminderUtils.addCalendarEvent(mContext,subFlagModel);
                }
            }
        }else {
            int random = (int)(Math.random()*100);
            eventId = Long.toString(System.currentTimeMillis()) +random;
            if(subFlagModel.isRemind){
                eventId = CalendarReminderUtils.addCalendarEvent(mContext,subFlagModel);
            }
        }
    }

    private boolean isNotificationEnabled() {
        boolean isOpened = false;
        try {
            isOpened = NotificationManagerCompat.from(mContext).areNotificationsEnabled();
        } catch (Exception e) {
            e.printStackTrace();
            isOpened = false;
        }
        return isOpened;

    }

    private void gotoSet() {

        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 26) {
            // android 8.0引导
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", mContext.getPackageName());
        } else if (Build.VERSION.SDK_INT >= 21) {
            // android 5.0-7.0
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", mContext.getPackageName());
            intent.putExtra("app_uid", mContext.getApplicationInfo().uid);
        } else {
            // 其他
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", mContext.getPackageName(), null));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);

    }


    private String status = "暂未开始";
    /**
     * 添加日程
     */
    private  void insertDayDBTable(String subIds){
        if(dayDBDao !=null){
            boolean isTicks ;
            if(status.equals("提前完成")){
                isTicks = true;
                    dayDBDao.deleteAllBySubId(eventId);
            }else {
                isTicks = false;
            }

            List<String> list = new ArrayList<>();
            if(TextUtils.isEmpty(subFlagModel.repeatDay)){
                list.add(subFlagModel.startTime);
            }else {
                String[] weeks = subFlagModel.repeatDay.split(" ");
                for (String week : weeks) {
                    list.addAll(DateUtils.getWeekly(subFlagModel.startTime,subFlagModel.endTime,week));
                }
            }
            //总打卡次数
//            XLog.e("------list----"+list.toString());
            subFlagModel.tick = list.size();
            if(TextUtils.isEmpty(subIds)){
                subFlagModel.subId = eventId;
//                if(status.equals("提前完成")){
//                    subFlagModel.progress = 1.0f;
//                    subFlagModel.tickNum= list.size();
//                }else {
//                    subFlagModel.progress = 0f;
//                }
                subFlagModel.createTime = System.currentTimeMillis();
                subFlagModelDao.insert(subFlagModel);
            }else {
                subFlagModel.subId = eventId;
//                if(status.equals("提前完成")){
//                    subFlagModel.progress = 1.0f;
//                    subFlagModel.tickNum= list.size();
//                }
                subFlagModelDao.update(subFlagModel);
            }

            for (String time : list) {
                DayTable dayTable = new DayTable();
                dayTable.day = time;
                dayTable.title = subFlagModel.title;
                dayTable.fId = flagId;
                dayTable.isTick = isTicks;
                dayTable.repeatTime = subFlagModel.repeatTime;
                dayTable.subId = eventId;
                dayTable.createTime = System.currentTimeMillis();
                if(status.equals("提前完成")){

                }else {
                    if(!time.equals(formatterDay.format(today))){
                        dayDBDao.insert(dayTable);
                    }else {
                        if(!todayIsTick){
                            dayDBDao.insert(dayTable);
                        }
                    }
                }
            }

            ModelInfo modelInfo = new ModelInfo();
            modelInfo.updateFlag = true;
            modelInfo.updateCalendar2 = true;
            modelInfo.subId = eventId;
            BusProvider.getBus().post(modelInfo);

        }
    }


    @Override
    protected void dialogDismiss() {
        dismiss();
    }

    @Override
    public void dismiss() {
        initSubFlagData();
        super.dismiss();

    }


}
